#!/bin/bash
set -e

host="127.0.0.1"
port=3306
user="root"
password=""
file=""
ssl_key=""
ssl_cert=""

while [[ ${1} ]]; do
	case "${1}" in
	--host)
		host=${2}
		shift
		;;
	--port)
		port=${2}
		shift
		;;
	--user)
		user=${2}
		shift
		;;
	--password)
		password=${2}
		shift
		;;
	--ssl-key)
		ssl_key=${2}
		shift
		;;
	--ssl-cert)
		ssl_cert=${2}
		shift
		;;
	*)
		file=${1}
		;;
	esac

	if ! shift; then
		echo 'Missing parameter argument.' >&2
		exit 1
	fi
done

cmd="mysql -u${user} -h${host} -P${port}"
if [[ -n ${password} ]]; then
	cmd="${cmd} -p${password}"
fi
if [[ -n ${ssl_key} ]]; then
	cmd="${cmd} --ssl-key=${ssl_key}"
fi
if [[ -n ${ssl_cert} ]]; then
	cmd="${cmd} --ssl-cert=${ssl_cert}"
fi
cmd="${cmd} --default-character-set utf8mb4 <${file}"

echo "will execute: $cmd"
eval $cmd
